// pages/search/search.js
const app = getApp();
import httpsUtils from '../../utils/https.js';
import myUtils from '../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    clickMsg: "搜索",
    keyWord: [],
    keywordArray: [],
    show: true
  },
  changeClick: {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var searchLog = [];
    if (!myUtils.isBlank(wx.getStorageSync("searchKey"))) {
      searchLog = wx.getStorageSync("searchKey");
    }
    //重新渲染数据
    this.setData({
      keyWord: searchLog,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 点击搜索
   */
  clickSearch: function (e) {
    var index = e.currentTarget.dataset.index;
    var searchLog = [];
    searchLog = wx.getStorageSync("searchKey");

    var content = searchLog.splice(index, 1);
    searchLog.unshift(content);
    wx.setStorageSync('searchKey', searchLog);

    //重新渲染数据
    this.setData({
      keyWord: searchLog,
    })
    wx.navigateTo({
      url: '../searchFocus/searchFocus'
    })
  },
  /**
   * 提交搜索
   */
  formSubmit: function (e) {
    var searchLog = [];
    if (myUtils.isBlank(e.detail.value.keyword)) {
      wx.showToast({
        title: '请输入关键词',
        icon:'none'
      })
      return;
    }
    if (!myUtils.isBlank(wx.getStorageSync("searchKey"))) {
      searchLog = wx.getStorageSync("searchKey");
    }
    //最多存储10条历史记录
    if (searchLog.length >= 10) {
      console.log(searchLog.length)
      searchLog.pop();
    }
    searchLog.unshift(e.detail.value.keyword);
    wx.setStorageSync('searchKey', searchLog);

    //重新渲染数据
    this.setData({
      keyWord: searchLog,
    })
    wx.navigateTo({
      url: '../searchFocus/searchFocus'
    })
  },
})